package com.hsy.crm.web.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.PageResult;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.ReceiptOrder;
import com.hsy.crm.web.domain.entity.Vendor;
import com.hsy.crm.web.domain.entity.VerificateOrder;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.domain.po.vendor.VendorGetPo;
import com.hsy.crm.web.domain.po.vendor.VendorPagePo;
import com.hsy.crm.web.domain.vo.vendor.VendorPageVo;
import com.hsy.crm.web.mapper.ReceiptOrderMapper;
import com.hsy.crm.web.mapper.VendorMapper;
import com.hsy.crm.web.mapper.VerificateOrderMapper;
import com.hsy.crm.web.service.base.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

@Service
public class VendorService extends BaseService<Vendor> {

    @Autowired
    VendorMapper vendorMapper;

    @Autowired
    ReceiptOrderMapper receiptOrderMapper;

    @Autowired
    VerificateOrderMapper verificateOrderMapper;

    public Unified<Integer> insertVendor(Vendor vendor) {
        Unified<Integer> unified = new Unified<>();
        //vendor.setBalance(0l);
        if(repeat_check_insert(vendor)){
            unified.setResult(App.BussError.CODE_REPEAT.getCode());
        }else{
            unified.setData(insert(vendor,Boolean.TRUE));
        }
        return unified;
    }

    public Unified<Integer> deleteVendor(Vendor vendor) {

        Long id = vendorMapper.checkOrder(vendor);
        if( id != null ){
            throw new ServiceException(App.BussError.VENDOR_DELETE_ORDER.getCode());
        }

        //receipt
        ReceiptOrder receiptOrder = new ReceiptOrder();
        receiptOrder.setCustomerId(vendor.getCustomerId());
        receiptOrder.setRelationId(vendor.getId());
        Long receiptId = receiptOrderMapper.checkOrder(receiptOrder);
        if( receiptId != null ){
            throw new ServiceException(App.BussError.VENDOR_DELETE_RECEIPT.getCode());
        }

        // verificate
        VerificateOrder verificateOrder = new VerificateOrder();
        verificateOrder.setCustomerId(vendor.getCustomerId());
        verificateOrder.setRelationId1(vendor.getId());
        Long verificateId = verificateOrderMapper.checkVendor(verificateOrder);
        if( verificateId != null ){
            throw new ServiceException(App.BussError.VENDOR_DELETE_HX.getCode());
        }

        return Unified.newInstance(this.delete(vendor));
    }

    public PageResult<VendorPagePo> page(VendorPageVo vendorPageVo) {
        Page page = PageHelper.startPage(vendorPageVo.getPage(),vendorPageVo.getLimit());
        PageResult<VendorPagePo> pageResult = new PageResult<>();

        if(vendorPageVo.getCompanyId()==null){
            vendorPageVo.setCompanyId(vendorPageVo.getLoginCompanyId());
        }

        pageResult.setRows(vendorMapper.page(vendorPageVo));
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    public Unified<VendorGetPo> getVendor(Vendor vendor) {
        Unified<VendorGetPo> unified = new Unified<>();
        unified.setData(vendorMapper.getVendor(vendor));
        return unified;
    }


    public Unified<Integer> updateVendor(Vendor vendor) {
        Unified<Integer> unified = new Unified<>();
        if(repeat_check_update(vendor)){
            unified.setResult(App.BussError.CODE_REPEAT.getCode());
        }else{
            unified.setData(update(vendor,Boolean.TRUE));
        }
        return unified;
    }

    private boolean repeat_check_insert(Vendor vendor){
        Example example = new Example(Vendor.class);
        example.setTableName( vendor.getDynamicTableName() );
        Example.Criteria criteria = example.createCriteria();
        if( StringUtils.isEmpty(vendor.getCode()) ){
            throw new ServiceException("bean field property name is null");
        }
        criteria.andEqualTo(App.Property.CODE,vendor.getCode());
        int count = vendorMapper.selectCountByExample(example);

        return count > 0;
    }

    private boolean repeat_check_update(Vendor vendor){
        Example example = new Example(Vendor.class);
        example.setTableName( vendor.getDynamicTableName() );
        Example.Criteria criteria = example.createCriteria();
        if( StringUtils.isEmpty(vendor.getCode()) || vendor.getCompanyId() == null || vendor.getId() == null ){
            throw new ServiceException("bean field property name,companyId is null");
        }
        criteria.andEqualTo(App.Property.CODE,vendor.getCode());
        criteria.andEqualTo(App.Property.COMPNAY_ID,vendor.getCompanyId());
        criteria.andNotEqualTo(App.Property.ID,vendor.getId());
        int count = vendorMapper.selectCountByExample(example);

        return count > 0;
    }

}
